package com.czh.subject.application.controller;

import com.alibaba.fastjson2.JSON;
import com.czh.auth.api.entity.ResponseResult;
import com.czh.subject.common.entity.PageResult;
import com.czh.subject.common.utils.UserUtil;
import com.google.common.base.Preconditions;
import com.czh.subject.application.convert.SubjectLikedDTOConverter;
import com.czh.subject.application.dto.SubjectLikedDTO;
import com.czh.subject.domain.entity.SubjectLikedBO;
import com.czh.subject.domain.service.SubjectLikedDomainService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 题目点赞表 controller
 *
 * @author caozaihui
 * @since 2024-09-14 11:56:34
 */
@RestController
@RequestMapping("/subjectLiked/")
@Slf4j
public class SubjectLikedController {

    @Resource
    private SubjectLikedDomainService subjectLikedDomainService;

    /**
     * 新增题目点赞表
     */
    @RequestMapping("add")
    public ResponseResult<Boolean> add(@RequestBody SubjectLikedDTO subjectLikedDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLikedController.add.dto:{}", JSON.toJSONString(subjectLikedDTO));
            }
            Preconditions.checkNotNull(subjectLikedDTO.getSubjectId(), "题目id不能为空");
            Preconditions.checkNotNull(subjectLikedDTO.getStatus(), "点赞状态不能为空");
            String userId = UserUtil.getUserId();
            Preconditions.checkNotNull(userId, "点赞人id不能为空");
            subjectLikedDTO.setLikeUserId(userId);
            SubjectLikedBO SubjectLikedBO = SubjectLikedDTOConverter.INSTANCE.convertDTOToBO(subjectLikedDTO);
            return ResponseResult.success(subjectLikedDomainService.add(SubjectLikedBO));
        } catch (Exception e) {
            log.error("SubjectLikedController.register.error:{}", e.getMessage(), e);
            return ResponseResult.error("新增题目点赞表失败");
        }
    }
    @PostMapping("getSubjectLikedPage")
    public ResponseResult<PageResult<SubjectLikedBO>> getSubjectLikedPage(@RequestBody SubjectLikedDTO subjectLikedDTO) {
        try {
            if (log.isInfoEnabled()){
                log.info("SubjectInfoController.getSubjectLikedPage.request:{}", JSON.toJSON(subjectLikedDTO));
            }
            SubjectLikedBO subjectLikedBO = new SubjectLikedBO();
            subjectLikedBO.setPageNo(subjectLikedBO.getPageNo());
            subjectLikedBO.setPageSize(subjectLikedBO.getPageSize());
            PageResult<SubjectLikedBO>  subjectLikedPage = subjectLikedDomainService.getMineSubjectLikedPage(subjectLikedBO);
            return ResponseResult.success(subjectLikedPage);
        }catch (Exception e){
            log.error("SubjectInfoController.getSubjectLikedPage.error:{}", e.getMessage(), e);
            return ResponseResult.error("查询点赞列表失败");
        }
    }
}
